<template>
  <!-- 登录表单 -->
  <el-form
    :model="form"
    status-icon
    :rules="rules"
    ref="form"
    label-width="100px"
    class="login-container"
  >
    <h3 class="login-title">商店后台管理系统登录</h3>
    <el-form-item
      label="用户名"
      label-width="80px"
      prop="username"
      class="username"
    >
      <!-- 用户名输入框 -->
      <el-input
        type="input"
        v-model="form.username"
        autocomplete="off"
        placeholder="请输入账号"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="密码"
      label-width="80px"
      prop="password"
    > 
      <!-- 密码输入框 -->
      <el-input
        type="password"
        v-model="form.password"
        autocomplete="off"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item class="login-submit"> 
      <!-- 登录按钮 -->
      <el-button type="primary" @click="login" class="login-submit">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getMenu } from '../../../api/data'
export default {
  name: "LoginShow",
  data() {
    return {
      form: {
        username:'',
        admin:''
      },
      rules: {
        // 表单验证规则
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            message: "用户名不能小于3位",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 登录方法
    login() {
      getMenu(this.form).then(res => {
        console.log(res)
        if (res.data.code === 20000) {
          console.log(this.$store)
          this.$store.commit('clearMenu')
          this.$store.commit('setMenu', res.data.data.menu)
          this.$store.commit('setToken', res.data.data.token)
          this.$store.commit('addMenu', this.$router)
          this.$router.push({name:'home'})
        } else {
          this.$message.warning(res.data.message)
        }
      })
      // const token = Mock.Random.guid()
      // this.$store.commit('setToken', token)
      // this.$router.push({name:'home'})
    }
  }
}
</script>

<style lang="less" scoped>
/* 登录容器样式 */
.login-container{
  border-radius: 15px;
  background-clip: padding-box;
  margin:180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background-color: #e7edee;
  border: 1px solid #230f48;
  box-shadow: 0 0 25px #f1eaea;
}

/* 登录标题样式 */
.login-title{
  margin: 0 auto 40px auto;
  text-align: center;
  color:#505458;
}

/* 登录按钮样式 */
.login-submit{
  margin: 10px auto 0px auto;
}
</style>



